package com.tecode.myBooks.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2018/11/21.
 */
public class DateUtil {

    private static SimpleDateFormat sdf;

    /**
     * 把日期转换为字符串
     *
     * @param date    需要转换的日期
     * @param pattern 转换的格式
     * @return
     */
    public static String DateToString(Date date, String pattern) {
        //实例化日期格式对象
        sdf = new SimpleDateFormat(pattern);

        return sdf.format(date);
    }

    /**
     * 字符串转换为日期
     * @param dateStr
     * @param pattern
     * @return
     * @throws ParseException
     */
    public static Date StringToDate(String dateStr, String pattern) throws ParseException {
        //实例化日期格式对象
        sdf = new SimpleDateFormat(pattern);

        return sdf.parse(dateStr);
    }


}
